package com.lanou.beautifulwardrobe.contants;

import android.app.DownloadManager;
import android.graphics.Bitmap;
import android.location.GpsStatus;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.beautifulwardrobe.Application.BaseApplication;
import com.lanou.beautifulwardrobe.entify.RecommedImage;
import com.lanou.beautifulwardrobe.entify.RecommendImageResult;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/21.
 */
public class VolleySingleton {
    private static VolleySingleton instance;
    private  RequestQueue queue;
    private ImageLoader imageLoader;


    private VolleySingleton() {
        queue = getQueue();
    }

    public  static VolleySingleton getInstance() {
        if (instance == null) {
            synchronized (VolleySingleton.class) {
                if (instance == null) {
                    instance = new VolleySingleton();
                }
            }
        }
        return instance;
    }

    private  RequestQueue getQueue() {
        if (queue == null) {
            queue = Volley.newRequestQueue(BaseApplication.getContext());
        }
        return queue;
    }
    public static final String TAG="VolleySingleton";
    public <T> void addRequest(Request<T> request){
        request.setTag(TAG);
        queue.add(request);
    }
    public <T> void addRequest(Request<T> request,Object tag){
        request.setTag(tag);
        queue.add(request);
    }
    public <T> void removeRequest(Object tag){
        queue.cancelAll(tag);

    }

    public void getStringRequset(String url,Response.Listener<String> listener){

            StringRequest request = new StringRequest(url, listener, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            }) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    // 添加请求头 在请求头中增加属性
                    Map<String, String> map = new HashMap<String, String>();
                    // 在请求头中添加apikey
                    map.put("Accept-Encoding", "");
                    return map;
                }

                @Override
                protected Response<String> parseNetworkResponse(
                        NetworkResponse response) {
                    try {
                        String jsonString = new String(response.data, "UTF-8");
                        return Response.success(jsonString,
                                HttpHeaderParser.parseCacheHeaders(response));
                    } catch (UnsupportedEncodingException e) {
                        return Response.error(new ParseError(e));
                    } catch (Exception je) {
                        return Response.error(new ParseError(je));
                    }
                }
            };
        instance.addRequest(request);
    }

    public void getStringRequset(String url,Response.Listener<String> listener,Object tag){

        StringRequest request = new StringRequest(url, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // 添加请求头 在请求头中增加属性
                Map<String, String> map = new HashMap<String, String>();
                // 在请求头中添加apikey
                map.put("Accept-Encoding", "");
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(
                    NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }
        };
        instance.addRequest(request,tag);
    }
    public void getImageResuest(String url,Response.Listener<Bitmap> listener){
        ImageRequest request =new ImageRequest(url, listener, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();;
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // 添加请求头 在请求头中增加属性
                Map<String, String> map = new HashMap<String, String>();
                // 在请求头中添加apikey
                map.put("Accept-Encoding", "");
                return map;
            }

        };
        instance.addRequest(request);
    }
}


